import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
class Student implements Cloneable{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class Test {
    public static int func1(){
        int a = 10;
        try{
            return a;
        }finally {
            a = 20;
            return a;
        }
    }
    public static void main(String[] args) {
        System.out.println("a = "+func1());
    }
    public static void main9(String[] args) {
        /*//error
        int[] arr = new int[]{1,2,3};
        try{
            System.out.println(arr[3]);
        }catch (Exception e){
            System.out.println("捕捉了Exception");
            e.printStackTrace();
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("捕捉了ArrayIndexOutOfBoundsException");
            e.printStackTrace();//打印异常的堆栈跟踪信息
        }
        System.out.println("try-catch执行完");*/
    }
    public static void main8(String[] args) {
        //多个异常需用多个catch块捕获
        int[] arr = new int[]{1,2,3};
        try{
            System.out.println(arr[3]);
            arr = null;
            System.out.println(arr[0]);
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("捕捉了ArrayIndexOutOfBoundsException");
        }catch (NullPointerException e){
            System.out.println("捕捉了NullPointerException");
        }
        System.out.println("try-catch执行完");
    }
    public static void main7(String[] args) {
        //异常类型不匹配
        int[] arr = new int[]{1,2,3};
        try{
            System.out.println(arr[3]);
            System.out.println(arr.length);
        }catch (NullPointerException e){
            System.out.println("捕捉了NullPointerException");
            e.printStackTrace();
        }
        System.out.println("try-catch执行完");
    }
    public static void main6(String[] args) {
        int[] arr = new int[]{1,2,3};
        try{
            System.out.println(arr[3]);
            System.out.println(arr.length);
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("捕捉了ArrayIndexOutOfBoundsException");
        }catch (NullPointerException e){
            System.out.println("捕捉了NullPointerException");
        }
        System.out.println("try-catch执行完");
    }
    public static int  funcc(int[] arr,int i){
        if(arr == null){
            throw new RuntimeException("该数组为null，产生空指针异常");
        }
        if(i >= arr.length || i <0){
            throw new ArrayIndexOutOfBoundsException("数组下标越界了,产生数组越界异常");
        }
        return arr[i];
    }

    public static void main5(String[] args) {
       /* //RuntimeException
        int[] arr = null;
        funcc(arr,0);*/
    }
    public static void main4(String[] args) {
        int[] arr = new int[]{1,2,3};
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        int a = funcc(arr,i);
        System.out.println(a);

    }
    public static void func(){
        func();
    }

    public static void main3(String[] args) {
        func();
    }

    public static void main2(String[] args) throws CloneNotSupportedException{
        Student student1 = new Student("小贺",20);
        Student student2 =(Student)student1.clone();

    }
    public static void main1(String[] args) {
        //算数异常
        int a = 10/0;
        System.out.println(a);
        /*//数组越界异常
        int[] arr = new int[]{1,2,3};
        System.out.println(arr[5]);*/
       /* //空指针异常
        int[] arr = null;
        System.out.println(arr[0]);*/
    }

}
